/**
 * 
 */
package com.sqltojson.service;

import java.util.Map;

import com.sqltojson.core.Query;
import com.sqltojson.core.Resultat;
import com.sqltojson.dao.ExecuteQueryDao;
import com.sqltojson.dao.QueryDao;
import com.sqltojson.exception.QueryDaoException;
import com.sqltojson.exception.QueryNotFoundException;
import com.sqltojson.exception.RequestServiceException;

/**
 * Implementation du requetage.
 */
public class RequestServiceImpl implements RequestService {

	/** Le dao pour les requetes.*/
	private QueryDao queryDao;
	/** L'executeur de requete.*/
	private ExecuteQueryDao executeQueryDao;
	
	/**
	 * Instancie les objets utilises dans le service.
	 */
	public RequestServiceImpl() {
	}

	/* (non-Javadoc)
	 * @see com.sqltojson.service.RequestService#query(java.lang.String, java.util.Map)
	 */
	public Resultat query(String nameQuery, Map<String, String> paramaters)
			throws QueryNotFoundException, RequestServiceException {
		try {
			final Query query = queryDao.get(nameQuery);
			if (query == null) {
				throw new QueryNotFoundException();
			}
			return executeQueryDao.find(query,
					paramaters);
		} catch (QueryDaoException e) {
			throw new RequestServiceException("Erreur lors de l'execution du service", e);
		}
	}
	
	public void setExecuteQueryDao(ExecuteQueryDao executeQueryDao) {
		this.executeQueryDao = executeQueryDao;
	}

	public void setQueryDao(QueryDao queryDao) {
		this.queryDao = queryDao;
	}
}
